/**
 * 保存立案地址
 * @param data
 * @param callback
 */
function saveCaseFillingPlaceOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_FILLINGPLACE(" +
    			"ADDRESS,ORGANID,ORGANNAME,CREATE_TIME) " +
    			"VALUES (?,?,?,?)",
    			[data.address==undefined ? '' : data.address  ,
				 data.organId==undefined ? '' : data.organId,
				 data.organName==undefined ? '' : data.organName,
				 data.createTime==undefined ? '' :data.createTime],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   message("立案地址保存失败");
	   });
    });
}

/**
 * 通过机构ID获取立案地址
 * @param organId 主键
 * @param callback 回调函数
 **/
function findCaseFillingPlaceByOrganIdOffLine(organId,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_FILLINGPLACE WHERE ORGANID = ? ORDER BY CREATE_TIME DESC", [organId], 
		function (tx, result){ 
/*		    var len = result.rows.length;  
		    if(len == 0){
		    	return;
		    }
		    
*/		    var data = setCaseFillingPlaceDataOffLine(result);
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
	    }, 
	    function(tx, ex) {
        });  
	});
}


/**
 * 通过立案地址和机构ID查验立案地址是否存在
 * @param address,organId 主键
 * @param callback 回调函数
 **/
function findCaseFillingPlaceByAddressAndOrganIdOffLine(address,organId,organName,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_FILLINGPLACE WHERE ADDRESS = ? AND ORGANID = ? ORDER BY CREATE_TIME DESC", [address,organId], 
		function (tx, result){ 
		    var len = result.rows.length;  
		    if(len > 0){
		    	return;
		    }else{
		    	var time = currentTime();
		    	var caseFillingPlace={};
		    	caseFillingPlace.address=address;
		    	caseFillingPlace.organId=organId;
		    	caseFillingPlace.organName=organName;
		    	caseFillingPlace.createTime=time;
		    	 if(typeof(callback) == 'function'){
		    	    	callback(caseFillingPlace);
		    	   }
		    }
	    }, 
	    function(tx, ex) {
        });  
	});
}
/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setCaseFillingPlaceDataOffLine(result){
	var len = result.rows.length;
	if(len == 0){
		return null;
	}
    var data = new Array();
	for (var i = 0; i < len; i++){
		var caseFillingPlace = {};
		caseFillingPlace.id = result.rows.item(i).CASE_FILLINGPLACE_ID;
		caseFillingPlace.address = result.rows.item(i).ADDRESS;
		caseFillingPlace.organId = result.rows.item(i).ORGANID;
		caseFillingPlace.organName = result.rows.item(i).ORGANNAME;
	    data.push(caseFillingPlace);
    }
	return data;
}

/**
 * 初始化立案地址
 * @param db
 * @param callback
 **/
function initCaseFillingPlace(){
	caseFillingPlace(getCaseFillingPlaceData);
}

/**
 * 清空数据
 */
function caseFillingPlace(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_FILLINGPLACE', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data,function(index,item){
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除立案地址失败");
		}); 
	});
}

/**
 * 从服务器上获取立案地址
 **/
function getCaseFillingPlaceData(casefillingPlaceData){
	if(casefillingPlaceData==null || casefillingPlaceData.length<=0){
		$.ajax({
			type : "get",
			url : getContextPath() + "/caseBasicInfo/getCaseFillingPlace",
			dataType : "json",
			data : {organId:$.cookie('organId')},
			error : function(request) {
				message("获取立案地址数据失败");
			},
			success : function(data) {
				$.each(data,function(index,item){
					saveCaseFillingPlaceOffLine(item);
				});
			}
		});
	}
}